﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using AdvancedRunAsClasses;
using Impersonation;

namespace AdvancedRunAsClient
{
    public partial class ClientForm : Form
    {
        public delegate void StartMainLoadDelegate();
        public bool debugEnabled = false;
        private static RegData cryptoRegistry;

        public ClientForm()
        {
            InitializeComponent();
            cryptoRegistry = new RegData();
            debugEnabled = Properties.Settings.Default.Debug;
            if(debugEnabled)
            {
                startTheAppToolStripMenuItem.Visible = true;
            }

        }

        private bool StartApp()
        {
            string dir = null;
            if (File.Exists(cryptoRegistry.Path))
            {
                dir = Path.GetDirectoryName(cryptoRegistry.Path);
            }
            else
            {
                LogMessage("Run Application", "File doesn't exists: " + cryptoRegistry.Path, ToolTipIcon.Error);
            }


            LogMessage("Run Application", "Trying to run app", ToolTipIcon.Info);
            notifyIcon1.Visible = false;
            this.Hide();
            if (Impersonator.LaunchNetOnly(cryptoRegistry.Path, cryptoRegistry.Domain, cryptoRegistry.User, cryptoRegistry.Password, dir))
            {
                notifyIcon1.Visible = true;
                LogMessage("Run Application", "Application run finished", ToolTipIcon.Info);
            }
            else
            {
                notifyIcon1.Visible = true;
                LogMessage("Run Application", "Application could not be started", ToolTipIcon.Error);
                return false;
            }
            return true;
        }



        private void LogMessage(string title, string message, ToolTipIcon icon)
        {
            notifyIcon1.ShowBalloonTip(10, title, message, icon);
            string txt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " : " + title + " - " + message + "\r\n";
            textBox1.AppendText(txt);
        }

        private bool ReadRegistry()
        {
            int res = cryptoRegistry.ReadRegistry();
            string errText = String.Empty;
            ToolTipIcon ico = ToolTipIcon.Error;
            bool result = false;
            switch (res)
            {
                case 0:
                    errText = "Data read: Success";
                    ico = ToolTipIcon.Info;
                    result = true;
                    break;
                case 1:
                    errText = "Data read: Error - no stored data found";
                    break;
                case 2:
                    errText = "Data read: Decryption Error - system was changed";
                    break;
                default:
                    errText = "Data read: Unknown error";
                    break;
            }
            LogMessage("Startup parameters", errText, ico);
            return result;
        }




        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (!this.Visible || (this.WindowState == FormWindowState.Minimized))
            {
                this.WindowState = FormWindowState.Normal;
                this.Show();
                this.BringToFront();
            }
            else
            {
                this.Hide();
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 frm = new AboutBox1();
            frm.ShowDialog();
            frm.Dispose();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void showLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.Show();
            this.BringToFront();
        }

        private void ClientForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (debugEnabled)
            {
                DialogResult dlgresult = MessageBox.Show("Really Quit?", "Exit", MessageBoxButtons.OKCancel);
                if (dlgresult == DialogResult.OK)
                { }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void startTheAppToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartMainLoad();
        }

        private void ClientForm_Load(object sender, EventArgs e)
        {
            if (!debugEnabled)
            {
                BeginInvoke(new StartMainLoadDelegate(StartMainLoad));
            }
        }

        public void StartMainLoad()
        {
            if (ReadRegistry())
            {
                StartApp();
            }
            if (!debugEnabled)
            {
                MessageBox.Show("Error while starting the Application", "AdvancedRunAs", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
        }
    }
}
